%% Filter stocks_id in same sector
clc
clear all

% Sector 1: Retail                % Sector 11: Chemistry
% Sector 2: Insurance             % Sector 12: Bank
% Sector 3: Real Estate           % Sector 13: Automobile
% Sector 4: IT                    % Sector 14: Natural Resources
% Sector 5: Oil & Gas             % Sector 15: FMCG
% Sector 6: Financial Services    % Sector 16: Media
% Sector 7: Electricity           % Sector 17: Telecom
% Sector 8: Hospitality           % Sector 18: Construction
% Sector 9: Heavy Industry        % Sector 19: Healthcare
% Sector 10: Household Products               

sector_id = 18;
all_sec_id_ok = get_sec_in_same_sector(sector_id);

%% Get financial ratios & normalize based on quantile

sub_sec_id_ok = all_sec_id_ok(1:end,1);

from_time = 2007;
to_time = 2012;

dataX = get_FI_from_security(sub_sec_id_ok,from_time,to_time);

dataX_normalize = normalize(dataX);
%% Preparation works for FA
% Parameters

time_lag = 1;   % in case of insider, change time_lag 1 or 0
%time_lag = 2;     % for normal investors

gap_ret = 2;      % compute return of 'gap_ret' days, always >=2
                  % (1-4: 5 gap_ret)
                 
[mat_X_FA, mat_Y_FA, mat_Y_FA_vol] = ...
    prepare_FA(dataX_normalize,gap_ret,time_lag);

mkdir([pwd '/hinh'],sprintf('Sector %d',sector_id))
%% Stepwise
%stepwise(mat_X_FA,mat_Y_FA);
%stepwise(mat_X_FA,mat_Y_FA_vol);
saveas(stepwise(mat_X_FA,mat_Y_FA),[pwd sprintf('/hinh/Sector %d/Stepwise_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
saveas(stepwise(mat_X_FA,mat_Y_FA_vol),[pwd sprintf('/hinh/Sector %d/Stepwise_vol_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
%% Tree
if (sector_id == 12)
    t = classregtree(mat_X_FA,mat_Y_FA,'names',{'roe', 'roa', 'EPS', 'pe', 'pb', 'NIM',...
        'P/R', 'credit_risk_pr', 'investment_pr', 'loan_losses_pr'},'minleaf', 50);
    %view(t)
    saveas(view(t),[pwd sprintf('/hinh/Sector %d/tree_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
    figure(4)
    plot(t.varimportance);
    set(gca, 'xticklabel', {'roe', 'roa', 'EPS', 'pe', 'pb', 'NIM',...
        'P/R', 'credit_risk_pr', 'investment_pr', 'loan_losses_pr'});
    saveas(figure(4),[pwd sprintf('/hinh/Sector %d/tree_varimportance_sector_%d_lag_%d.jpg', sector_id, sector_id,time_lag)]);
elseif (sector_id == 2)
    t = classregtree(mat_X_FA,mat_Y_FA,'names',{'roe', 'roa', 'EPS', 'pe', 'pb', 'GPM', 'P/R',...
        'well_liquid', 'current_liabilities', 'long_term_asset',...
        'insurance_sale', 'gross_insurance_income'},'minleaf', 50);
    saveas(view(t),[pwd sprintf('/hinh/Sector %d/tree_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
    figure(4)
    plot(t.varimportance);
    set(gca, 'xticklabel', {'roe', 'roa', 'EPS', 'pe', 'pb', 'GPM', 'P/R',...
        'well_liquid', 'current_liabilities', 'long_term_asset',...
        'insurance_sale', 'gross_insurance_income'});
    saveas(figure(4),[pwd sprintf('/hinh/Sector %d/tree_varimportance_sector_%d_lag_%d.jpg', sector_id, sector_id,time_lag)]);
elseif (sector_id == 6)
    t = classregtree(mat_X_FA,mat_Y_FA,'names',{'roe', 'roa', 'EPS', 'pe', 'pb', 'GPM', 'P/R',...
        'well_liquid', 'interest_ratio', 'current_liabilities', 'long_term_asset'},'minleaf', 50);
    saveas(view(t),[pwd sprintf('/hinh/Sector %d/tree_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
    figure(4)
    plot(t.varimportance);
    set(gca, 'xticklabel', {'roe', 'roa', 'EPS', 'pe', 'pb', 'GPM', 'P/R',...
        'well_liquid', 'interest_ratio', 'current_liabilities', 'long_term_asset'});
    saveas(figure(4),[pwd sprintf('/hinh/Sector %d/tree_varimportance_sector_%d_lag_%d.jpg', sector_id, sector_id,time_lag)]);
else
    t = classregtree(mat_X_FA,mat_Y_FA,'names',{'ROE','ROA','EPS','P/E','P/B',...
        'GPM','P/R','COTA','quick ratio','cash ratio',...
        'interest coverage','debt ratio','inventory ratio'},'minleaf', 50);
    %view(t)
    saveas(view(t),[pwd sprintf('/hinh/Sector %d/tree_sector_%d_lag_%d.jpg', sector_id,sector_id,time_lag)]);
    figure(4)
    plot(t.varimportance);
    set(gca, 'xticklabel', {'ROE','ROA','EPS','P/E','P/B',...
        'GPM','P/R','COTA','quick ratio','cash ratio',...
        'interest coverage','debt ratio','inventory ratio'});
    saveas(figure(4),[pwd sprintf('/hinh/Sector %d/tree_varimportance_sector_%d_lag_%d.jpg', sector_id, sector_id,time_lag)]);
end
